package com.hanxing.kxcharge.module.operations.service.station;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.hanxing.kxcharge.module.operations.controller.admin.station.vo.*;
import com.hanxing.kxcharge.module.operations.dal.dataobject.station.StationDO;
import com.hanxing.kxcharge.framework.common.pojo.PageResult;
import com.hanxing.kxcharge.framework.common.pojo.PageParam;
import com.hanxing.kxcharge.framework.common.util.object.BeanUtils;

import com.hanxing.kxcharge.module.operations.dal.mysql.station.StationMapper;

import static com.hanxing.kxcharge.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hanxing.kxcharge.module.operations.enums.ErrorCodeConstants.*;

/**
 * 站点信息 Service 实现类
 *
 * @author 航兴充电
 */
@Service
@Validated
public class StationServiceImpl implements StationService {

    @Resource
    private StationMapper stationMapper;

    @Override
    public Long createStation(StationSaveReqVO createReqVO) {
        // 插入
        StationDO station = BeanUtils.toBean(createReqVO, StationDO.class);
        stationMapper.insert(station);
        // 返回
        return station.getId();
    }

    @Override
    public void updateStation(StationSaveReqVO updateReqVO) {
        // 校验存在
        validateStationExists(updateReqVO.getId());
        // 更新
        StationDO updateObj = BeanUtils.toBean(updateReqVO, StationDO.class);
        stationMapper.updateById(updateObj);
    }

    @Override
    public void deleteStation(Long id) {
        // 校验存在
        validateStationExists(id);
        // 删除
        stationMapper.deleteById(id);
    }

    private void validateStationExists(Long id) {
        if (stationMapper.selectById(id) == null) {
            throw exception(STATION_NOT_EXISTS);
        }
    }

    @Override
    public StationDO getStation(Long id) {
        return stationMapper.selectById(id);
    }

    @Override
    public PageResult<StationDO> getStationPage(StationPageReqVO pageReqVO) {
        return stationMapper.selectPage(pageReqVO);
    }

}